package com.codingxyz.simple.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

        Class rt = Class.forName("com.codingxyz.simple.reflect.Robot");
        Robot robot = (Robot) rt.newInstance();
        System.out.println("class name is "+rt.getName());

        /**
         * 访问类的方法，public,private,protect ,继承类的方法和实现类的方法不能访问
         */
        Method method = rt.getDeclaredMethod("throwHello",String.class);
        method.setAccessible(true);
        Object str = method.invoke(robot,"Bob");
        System.out.println("getHello result is : "+str);

        /**
         * 只能获取public方法，及继承的public方法
         */
        Method sayHi = rt.getMethod("sayHi",String.class);
        sayHi.invoke(rt,"Tim ");
    }
}
